package com.baike.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.baike.model.FileEntity;
import com.baike.model.FileObj;
import com.baike.model.HibernateUtil;
import com.baike.model.UserEntity;

public class FileDao {
	PreparedStatement ps;
	ResultSet rs;

	// 涓婁紶鏂囦欢
	public void upload(FileObj file) {
		Session session = null;
	FileEntity fileentity=new FileEntity();
	fileentity.setAuthor_id(file.getAuthor_id());
	fileentity.setDownload_times(0);
	fileentity.setEntry_id(file.getEntry_id());
	fileentity.setIs_pass(false);
	//fileentity.setLast_time(new Date(new java.util));
	fileentity.setName(file.getFile_name());
	fileentity.setPath(file.getPath());
	fileentity.setUpload_time(new Date(new java.util.Date().getTime()));
	try{
		session=HibernateUtil.getSession();
		session.beginTransaction();
		session.persist(fileentity);
		session.getTransaction().commit();
	}catch (Exception e) {
		// TODO: handle exception
		session.getTransaction().rollback();
		e.printStackTrace();
		System.out.println("数据添加失败");
	}finally {
		HibernateUtil.closeSession();
	}
	}
	/**根据id获得文件实例
	 * 
	 * @param id 
	 * @return
	 */
	public static FileEntity getFile(int id){
		String hql="from FileEntity file where file.id="+id;
		Session session;
		List<FileEntity> filelist=new ArrayList<>();
		try{
			session=HibernateUtil.getSession();
			Query q=session.createQuery(hql);
			filelist=q.list();
			if(!filelist.isEmpty())
				return filelist.get(0);
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{
			HibernateUtil.closeSession();
		}
		return null;
	}
	/**根据id从数据库中获取真实路径
	 * 
	 * @param id 文件id
	 * @return 真实路径
	 */
	public static String getPathById(int id){
		return getFile(id).getPath();
	}
	/**更新下载次数和最后下载时间
	 * 
	 * @param id
	 */
	public static void downloadOnce(int id){
		FileEntity file=getFile(id);
		Session session = null;
		file.setDownload_times(file.getDownload_times()+1);
		file.setLast_time(new Date(new java.util.Date().getTime()));
		try{
			session=HibernateUtil.getSession();
			session.beginTransaction();
			session.update(file);
			session.getTransaction().commit();
		}catch (Exception e) {
			// TODO: handle exception
			session.getTransaction().rollback();
			e.printStackTrace();
		}finally {
			HibernateUtil.closeSession();
		}
	}
	/**根据词条id返回文件的list集合
	 * @param id 词条id
	 * @return 文件List集合
	 */
	@SuppressWarnings("finally")
	public static List<FileEntity> getAllFileById(int id){
		String hql="from FileEntity file where file.entry_id="+id+" and file.is_pass=1";
		Session session = null;
		List<FileEntity> filelist=new ArrayList<>();
		try{
			session=HibernateUtil.getSession();
			Query q=session.createQuery(hql);
			filelist=q.list();
		
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{			
			// session.close();
			 //System.out.println(filelist.size());
			HibernateUtil.closeSession();
			return filelist;
		}
	}
	/**返回所有未审查的文件
	 * 
	 * @return
	 */
	@SuppressWarnings("finally")
	public static List<FileEntity> getAllUncheckedFile(){
		String hql="from FileEntity file where file.is_pass=0";
		Session session = null;
		List<FileEntity> filelist=new ArrayList<>();
		try{
			session=HibernateUtil.getSession();
			Query q=session.createQuery(hql);
			filelist=q.list();
		
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}finally{			
			HibernateUtil.closeSession();
			return filelist;
		}
	}
	public static void main(String[] args) {
		System.out.println(getAllUncheckedFile().size());
	}
}
